package com.bigfreakindeals.Activities;

import com.bigfreakindeals.R;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Common.Tools;
import com.bigfreakindeals.Controller.AsynchListener;
import com.bigfreakindeals.Models.BeansSays;

import android.app.Activity;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class BeansSaysActivity extends Activity implements OnClickListener {

	String beansSays = "<p>The history of Spring Break dates all the way back to 1793 when President George Washington first traveled to Cancun to get wasted at Senor Frogs. On his voyage home, he spent two nights in Panama City Beach, where he spent the very first dollar with his picture on it at a local Hooters. They still have that dollar bill up behind the counter&mdash; true story.</p>";
	String beanSaysTitle = "History of Spring Break";

	LinearLayout headertopBG;
	LinearLayout headerbottomBG;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.beanssay);
	
		
		
	}

	public void updateUniversityTheme() {
		
		/*if (ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getSchoolTitle() != null) {
			TextView headerTitle = (TextView) findViewById(R.id.header_title);
			headerTitle.setText(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getSchoolTitle());
			headerTitle.setTextColor(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().color_school_font+ (-16777216));
			headerTitle.setOnClickListener(this);
			headerTitle.setClickable(true);
		}

		headertopBG = (LinearLayout) findViewById(R.id.headerbg_top);
		headerbottomBG = (LinearLayout) findViewById(R.id.headerbg_bottom);
		
		if (ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getColor_titleBarBottomColor() > -1
				&& ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getColor_titleBarTopColor() > -1) {
			if (headerbottomBG != null)
				headerbottomBG.setBackgroundColor(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getColor_titleBarBottomColor()
						+ (-16777216));

			if (headertopBG != null)
				headertopBG
						.setBackgroundColor(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getColor_titleBarTopColor() + (-16777216));
		}

		// whole content page
		((RelativeLayout) findViewById(R.id.container))
				.setBackgroundColor(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().color_school_bg + (-16777216));

		// gradient gray
		((RelativeLayout) findViewById(R.id.beansayspage)).setBackgroundDrawable(ApplicationEx.getInstance().gradientGray_TOP_BOTTOM_ACTIVEDEALS);

		// white Sc
		((ScrollView) findViewById(R.id.deallistScroll)).setBackgroundColor(0xffffffff);
		
		((ImageView) findViewById(R.id.button_beansays)).setImageDrawable(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().drawablImages.drawable_beansay_text);
		
		((ImageView) findViewById(R.id.beandog)).setImageDrawable(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().drawablImages.drawable_beansay_dog);
		((ImageView) findViewById(R.id.beandog)).bringToFront();


		*/
		if (ApplicationEx.getInstance().myProfile.getlastSelectedSchool()!=null && ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getSchoolTitle() != null) {
			TextView headerTitle = (TextView) findViewById(R.id.header_title);
			headerTitle.setText(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getSchoolTitle());
			headerTitle.setTextColor(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().color_school_font+ (-16777216));
			headerTitle.setOnClickListener(this);
			headerTitle.setClickable(true);
		}

		headertopBG = (LinearLayout) findViewById(R.id.headerbg_top);
		headerbottomBG = (LinearLayout) findViewById(R.id.headerbg_bottom);
		
		if (ApplicationEx.getInstance().myProfile.getlastSelectedSchool()!= null && ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getColor_titleBarBottomColor() > -1
				&& ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getColor_titleBarTopColor() > -1) {
			if (headerbottomBG != null)
				headerbottomBG.setBackgroundColor(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getColor_titleBarBottomColor()
						+ (-16777216));

			if (headertopBG != null)
				headertopBG
						.setBackgroundColor(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getColor_titleBarTopColor() + (-16777216));
		}

		// whole content page
		((RelativeLayout) findViewById(R.id.container))
				.setBackgroundColor(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().color_school_bg + (-16777216));

		// gradient gray
		((RelativeLayout) findViewById(R.id.beansayspage)).setBackgroundDrawable(ApplicationEx.getInstance().gradientGray_TOP_BOTTOM_ACTIVEDEALS);

		// white Sc
		((ScrollView) findViewById(R.id.deallistScroll)).setBackgroundColor(0xffffffff);
		
		//color if the image and the bean text follows the selected school
		//color of the background follows the Home schools
		if (ApplicationEx.getInstance().myProfile.getlastSelectedSchool()!= null && ApplicationEx.getInstance().myProfile.getlastSelectedSchool().drawablImages!=null){
			((ImageView) findViewById(R.id.button_beansays)).setImageDrawable(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().drawablImages.drawable_beansay_text);
			((ImageView) findViewById(R.id.beandog)).setImageDrawable(ApplicationEx.getInstance().myProfile.getlastSelectedSchool().drawablImages.drawable_beansay_dog);
		}else {
			((ImageView) findViewById(R.id.button_beansays)).setImageDrawable(null);
			((ImageView) findViewById(R.id.beandog)).setImageDrawable(null);
		}
		((ImageView) findViewById(R.id.beandog)).bringToFront();

	}
	
	public void noBeansSaysContent() {
		updateUniversityTheme();

	}

	public void updateBeansContent() {

		if (BeansSays.getInstance().getTitle() != null && BeansSays.getInstance().getComment() != null
				&& BeansSays.getInstance().getDatecreated() != null) {

			updateUniversityTheme();
			
			((TextView) findViewById(R.id.beansaystitle)).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.beansaystext)).setVisibility(View.VISIBLE);
			
			((TextView) findViewById(R.id.beansaystitle)).setText(beanSaysTitle);
			((ImageView) findViewById(R.id.beandog)).bringToFront();

			Spanned marked_up = null;

			
			marked_up = Html.fromHtml(beansSays);
			if (marked_up != null)
				((TextView) findViewById(R.id.beansaystext)).setText(marked_up);

		} else {
			noBeansSaysContent();

		}

	}

	public void unregister() {

		this.unregisterReceiver(beansSayBroadcastReceiver);
	}

	public BroadcastReceiver beansSayBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			
			if (intent.getAction().equals(Constant.EVENTS_GETBEANSSAYS)) {
				((RelativeLayout) findViewById(R.id.container)).setVisibility(View.VISIBLE);
				((ImageView) findViewById(R.id.beandog)).setVisibility(View.VISIBLE);
				((ProgressBar) findViewById(R.id.progress)).setVisibility(View.INVISIBLE);

				((RelativeLayout) findViewById(R.id.container)).setVisibility(View.VISIBLE);
				((ImageView) findViewById(R.id.beandog)).setVisibility(View.VISIBLE);

				if (BeansSays.getInstance().getTitle() != null && BeansSays.getInstance().getComment() != null) {
					beanSaysTitle = BeansSays.getInstance().getTitle();
					beansSays = BeansSays.getInstance().getComment();
					updateBeansContent();

				} else
					noBeansSaysContent();

			} else if (intent.getAction().equals(Constant.EVENTS_GETBEANSSAYS_ERROR)) {
				((RelativeLayout) findViewById(R.id.container)).setVisibility(View.VISIBLE);
				((ImageView) findViewById(R.id.beandog)).setVisibility(View.VISIBLE);
				((ProgressBar) findViewById(R.id.progress)).setVisibility(View.INVISIBLE);

				noBeansSaysContent();
			}else if (intent.getAction().equals(Constant.EVENTS_UPDATESCREEN)) {
				updateBeansContent();
			}
		}
	};

	/*public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return false;

		}
		return super.onKeyDown(keyCode, event);

	}*/

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResume() {
		super.onResume();
		
		this.registerReceiver(beansSayBroadcastReceiver, new IntentFilter(Constant.EVENTS_GETBEANSSAYS));
		this.registerReceiver(beansSayBroadcastReceiver, new IntentFilter(Constant.EVENTS_GETBEANSSAYS_ERROR));
		this.registerReceiver(beansSayBroadcastReceiver, new IntentFilter(Constant.EVENTS_UPDATESCREEN));

		BeansSays.getInstance().clearBeansSay();

		((ProgressBar) findViewById(R.id.progress)).setVisibility(View.VISIBLE);

		
		// all item visible()
		((RelativeLayout) findViewById(R.id.container)).setVisibility(View.VISIBLE);
		
		((ImageView) findViewById(R.id.beandog)).setVisibility(View.VISIBLE);

		((View) findViewById(R.id.separator_top)).setVisibility(View.VISIBLE);
		((TextView) findViewById(R.id.beansaystitle)).setVisibility(View.INVISIBLE);
		((TextView) findViewById(R.id.beansaystext)).setVisibility(View.INVISIBLE);
		
		((TextView) findViewById(R.id.header_title)).setVisibility(View.VISIBLE);
		
		findViewById(R.id.separator_top).setBackgroundDrawable(
				Tools.getDrawableButtonGeneric(new int[] { 0xffa8a8a8,
						0xffcacaca }, 0, GradientDrawable.Orientation.TOP_BOTTOM));

		updateUniversityTheme();
	
		
		ApplicationEx.getInstance().controller.getBeansSays();

		// TODO Auto-generated method stub

	}


}
